package com.ruoyi.jwyz.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.jwyz.domain.Activity;
import com.ruoyi.jwyz.domain.UserAttendActivity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.jwyz.domain.Post;
import com.ruoyi.jwyz.service.IPostService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 用户动态Controller
 *
 * @Author lxp
 * @Date 2023-03-31
 */
@Api(tags = "用户动态")
@RestController
@RequestMapping("/jwyz/post")
public class PostController extends BaseController {
    @Autowired
    private IPostService postService;

    /**
     * 查询用户动态列表
     */
    @ApiOperation("查询用户动态列表")
//    @PreAuthorize("@ss.hasPermi('jwyz:post:list')")
    @GetMapping("/list")
    public TableDataInfo list(Post post) {
        startPage();
        List<Post> list = postService.selectPostList(post);
        return getDataTable(list);
    }

    /**
     * 导出用户动态列表
     */
    @ApiOperation("导出用户动态列表")
//    @PreAuthorize("@ss.hasPermi('jwyz:post:export')")
    @Log(title = "用户动态", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Post post) {
        List<Post> list = postService.selectPostList(post);
        ExcelUtil<Post> util = new ExcelUtil<Post>(Post.class);
        util.exportExcel(response, list, "用户动态数据");
    }

    /**
     * 获取用户动态详细信息
     */
    @ApiOperation("获取用户动态详细信息")
//    @PreAuthorize("@ss.hasPermi('jwyz:post:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam(value = "用户动态ID", required = true) @PathVariable("id") Long id) {
        return success(postService.selectPostById(id));
    }


    /**
     * 新增用户动态
     */
    @ApiOperation("新增用户动态")
//    @PreAuthorize("@ss.hasPermi('jwyz:post:add')")
    @Log(title = "用户动态", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Post post) {
        return toAjax(postService.insertPost(post));
    }

    /**
     * 修改用户动态
     */
    @ApiOperation("修改用户动态")
//    @PreAuthorize("@ss.hasPermi('jwyz:post:edit')")
    @Log(title = "用户动态", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Post post) {
        return toAjax(postService.updatePost(post));
    }

    /**
     * 删除用户动态
     */
    @ApiOperation("删除用户动态")
//    @PreAuthorize("@ss.hasPermi('jwyz:post:remove')")
    @Log(title = "用户动态", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@ApiParam(value = "用户动态IDs", required = true) @PathVariable Long[] ids) {
        return toAjax(postService.deletePostByIds(ids));
    }

    @ApiOperation("获取用户发布动态的详细信息")
//    @GetMapping("/getDynamic/{uid}")
    public AjaxResult getDynamic(@PathVariable("uid") String uid) {
        Long userId = Long.parseLong(uid);
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<Post> postList=postService.list(queryWrapper);
        if (postList.size() != 0) {
            return success(postList);
        }
        return error("没有发布相关动态信息");
    }

}

